'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 竞技场NPC信息
 * @author agl
 */

var _ = require('lodash');
var soldierAttr = require('./soldierAttr');
var buildAttr = require('./buildAttr');
var buildType = require('../../consts/buildType');
var buildConf = require('../../config/buildConf');
var arenaConf = require('../../config/arenaConf.js');
var heroConf = require('../../config/heroConf');
var constantUtil = require('../../util/constantUtil');
var buildTypeUtil = require('../../util/buildTypeUtil');
var techService = require('../../services/techService');
var buildService = require('../../services/buildService.js');
var dataApi = require('../../libs/dataApi.js');

/**
 * 获取竞技场NPC信息
 * @param  {[type]} npc_id [description]
 * @return {[type]}         [description]
 */
exports.getData = function (npc_id) {
    var npc_info = arenaConf.getArenaConf(npc_id);
    return {
        name: npc_info.name,
        soldier_list: _getSoldierList(npc_info.soldier_info),
    };
};

/**
 * 获取士兵列表
 * @param  {[type]} soldier_info_str       [description]
 * @param  {[type]} soldier_ratio       [description]
 * @return {[type]}               [description]
 */
var _getSoldierList = function (soldier_info_str) {
    // var soldier_ratio = 0;
    var soldier_list = [];
    var soldier_type_lv_list = {};
    var ratio_keys = ['min_atk', 'max_atk', 'fire_damage', 'ice_damage', 'light_damage', 'city_damage', 'hp'];

    var batch_soldier_list = _parseSoldierList(soldier_info_str);
    _.forEach(batch_soldier_list, function (soldier_item) {
        if (!soldier_item) {
            soldier_list.push(null);
            return true;
        }
        var soldier_type = soldier_item.soldier_type;
        var soldier_lv = soldier_item.soldier_lv;
        var soldier_star = soldier_item.soldier_star;

        var soldier_info = soldierAttr.getAttr(
            soldier_type,
            soldier_lv,
            soldier_star
        );
        if (!soldier_info) {
            soldier_list.push(null);
            return true;
        }

        // if (!!soldier_ratio) {
        //     _.forEach(ratio_keys, function (fld) {
        //         soldier_info.attr[fld] = Math.ceil(soldier_info.attr[fld] * soldier_ratio / 100);
        //     });
        // }

        soldier_list.push(soldier_info);
    });

    return soldier_list;
};

/**
 * 解析士兵信息
 * @return {Array} [description]
 */
var _parseSoldierList = function (soldier_info_str) {
    var soldier_list = [];
    if (soldier_info_str.length <= 0) {
        return soldier_list;
    }

    var soldier_info_list = soldier_info_str.split(',');
    _.forEach(soldier_info_list, function (soldier_info_item) {
        var soldier_info = soldier_info_item.split(':');
        var soldier_type = parseInt(soldier_info[0]);
        var soldier_lv = parseInt(soldier_info[1]);
        var soldier_star = parseInt(soldier_info[2]);

        if (!!soldier_type) {
            soldier_list.push({
                soldier_type: soldier_type,
                soldier_lv: soldier_lv,
                soldier_star: soldier_star
            });
        } else {
            soldier_list.push(null);
        }
    });
    return soldier_list;
};
